import axios from 'axios'
import Vue from '../main'
import {Loading} from 'element-ui';
import {baseURL} from "../util";

let instance = axios.create({
    withCredentials: true
});
instance.timeout = 8000;
// instance.defaults.baseURL = process.env.NODE_ENV === 'production' ? 'http://47.100.240.34:8093' : 'http://192.168.1.204:8080';   //106
instance.defaults.baseURL = baseURL;
instance.defaults.headers = {"Content-Type" : "application/x-www-form-urlencoded"};
const options = {
    text:'Processing...'
};
let loadingInstance ;
instance.interceptors.request.use((config)=>{
    loadingInstance = Loading.service(options);
    return config;
},err=>{
    return Promise.reject(err);
});

instance.interceptors.response.use(( response ) => {
    // 对响应数据做点什么
    if (response.data.status === 10) {
        Vue.$msgbox({
            title:'Warning',
            message:'Please Login!'
        });
        Vue.$router.replace('/login')
    }
    loadingInstance.close()
    return response;
}, ( error ) => {
    if (error.message.indexOf('500')>-1) {
        Vue.$notify.error('500 Internal Server Error,Please contact Administrator')
    }

    // 对响应错误做点什么
    // alert('Internal Server Error');
    loadingInstance.close();
    return Promise.reject(error);

});

export default instance;